﻿using System;
using System.Data;
using System.Collections.Generic;

using SobFS.Model;
using SobFS.DALFactory;
using SobFS.IDAL;
using SobFS.Common.Database;
using SobFS.Common;

namespace SobFS.BLL
{
	/// <summary>
	/// SysOrganization
	/// </summary>
	public partial class SysOrganization
	{
		private readonly ISysOrganization dal=DataAccess.CreateSysOrganization();
		public SysOrganization()
		{}

        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            return dal.Exists(Id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(SobFS.Model.SysOrganization model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SobFS.Model.SysOrganization model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            return dal.Delete(Id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return dal.DeleteList(Idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SobFS.Model.SysOrganization GetModel(int Id)
        {

            return dal.GetModel(Id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public SobFS.Model.SysOrganization GetModelByCache(int Id)
        {
            string cacheKey = typeof(SobFS.Model.SysOrganization).Name + Id;
            object objModel = DbCacheHelper.GetCache(cacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(Id);
                    if (objModel != null)
                    {
                        int modelCache = ConfigHelper.GetConfigInt("modelCache");
                        DbCacheHelper.SetCache(cacheKey, objModel, DateTime.Now.AddMinutes(modelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (SobFS.Model.SysOrganization)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<SobFS.Model.SysOrganization> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<SobFS.Model.SysOrganization> DataTableToList(DataTable dt)
        {
            List<SobFS.Model.SysOrganization> modelList = new List<SobFS.Model.SysOrganization>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                SobFS.Model.SysOrganization model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new SobFS.Model.SysOrganization();
                    if (dt.Rows[n]["Id"] != null && dt.Rows[n]["Id"].ToString() != "")
                    {
                        model.Id = int.Parse(dt.Rows[n]["Id"].ToString());
                    }
                    if (dt.Rows[n]["ParentId"] != null && dt.Rows[n]["ParentId"].ToString() != "")
                    {
                        model.ParentId = int.Parse(dt.Rows[n]["ParentId"].ToString());
                    }
                    if (dt.Rows[n]["Code"] != null && dt.Rows[n]["Code"].ToString() != "")
                    {
                        model.Code = dt.Rows[n]["Code"].ToString();
                    }
                    if (dt.Rows[n]["Name"] != null && dt.Rows[n]["Name"].ToString() != "")
                    {
                        model.Name = dt.Rows[n]["Name"].ToString();
                    }
                    if (dt.Rows[n]["Position"] != null && dt.Rows[n]["Position"].ToString() != "")
                    {
                        model.Position = int.Parse(dt.Rows[n]["Position"].ToString());
                    }
                    if (dt.Rows[n]["IsObsoleted"] != null && dt.Rows[n]["IsObsoleted"].ToString() != "")
                    {
                        if ((dt.Rows[n]["IsObsoleted"].ToString() == "1") || (dt.Rows[n]["IsObsoleted"].ToString().ToLower() == "true"))
                        {
                            model.IsObsoleted = true;
                        }
                        else
                        {
                            model.IsObsoleted = false;
                        }
                    }
                    if (dt.Rows[n]["IsEnabled"] != null && dt.Rows[n]["IsEnabled"].ToString() != "")
                    {
                        if ((dt.Rows[n]["IsEnabled"].ToString() == "1") || (dt.Rows[n]["IsEnabled"].ToString().ToLower() == "true"))
                        {
                            model.IsEnabled = true;
                        }
                        else
                        {
                            model.IsEnabled = false;
                        }
                    }
                    if (dt.Rows[n]["Description"] != null && dt.Rows[n]["Description"].ToString() != "")
                    {
                        model.Description = dt.Rows[n]["Description"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

       
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  Method
    }
}

